<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 <%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>

		<link rel="stylesheet" href="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/css/bootstrap.min.css">
		<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
		<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>

<script type="text/javascript" src="<%=basePath%>Js/jquery.js"></script>
<link rel="stylesheet" type="text/css" href="<%=basePath%>css/mycss/bootstrap.min.css" />
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<script type="text/javascript">

//下架商品
function soldOut(){
	
	var supermarketId = $("#supermarketId").val();
	
	$.ajax({
		type:"post",
		url:"soldOut.do",
		data:{
			"id":supermarketId,
		},
		dataType:"text",
		success:function(result){
			if(result == "1"){
				alert("商品下架成功")
				var del = $("#"+supermarketId+"");
				del.remove();
			}else{
				alert("商品下架失败")
			}
		},
		error:function(){
			alert("系统故障")
		},
	})
	
}

//添加超市商品数量
function addquantity(){
	
	var supermarketId = $("#supermarketId").val();
	var addquantity = $("#addquantity").val();
	var quantity = $("#quantity").val();
	
	if (!(/(^[0-9]*[1-9][0-9]*$)/.test(addquantity))){
		alert("请输入正整数")
	}else{
		$.ajax({
			 type:"post",
			 url:"addSuperquantity.do",
			 async: false,
			 data:{
				 "supermarketId":supermarketId,
				 "addquantity":addquantity,
				 "quantity":quantity,
			 },
			 dataType:"text",
			 success:function(result){
				 if(result == "1"){
					 alert("添加成功")
					 window.location.href="";
				 }else{
					 alert("添加失败")
				 }
			 },
			 error:function(){
				 alert("系统故障")
			 }
			   
		   })
	}

   
	
}

//获取商品信息--模态框
function queryProutInf(id,judge){
	
	$.ajax({
		type:"post",
		url:"selectSupermarketById.do",
		data:{
			"id":id,
		},
		success:function(result){
			stus = JSON.parse(result)
			var stu = ""
			for(var i = 0; i < stus.length; i++ ){
				stu +="<tr>";
				stu +="<td>商品名称：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='product' value='"+stus[i].product+"'>";
				stu +="<input type='hidden' class='form-control' id='supermarketId' value='"+stus[i].reserved1+"'></td>";
				stu +="<td>商品分类：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='classify' value='"+stus[i].classify+"'></td>";
				stu +="</tr>";
				stu +="<tr>";
				stu +="<td>单价：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='price' value='"+stus[i].price+"￥'></td>";
				stu +="<td>数量：</td>";
				stu +="<td><input type='text' readonly='readonly' class='form-control' id='quantity' value='"+stus[i].quantity+"'></td>";
				stu +="</tr>";
			}
			if(judge == 1){
				stu2 = "<h4 align='center'>商品下架</h4>"
			}else{
				stu2 = "<h4 align='center'>添加商品数量</h4>"
				stu +="<tr>";
				stu +="<td>添加数量：</td>";
				stu +="<td><input type='text' class='form-control' id='addquantity' ></td>";
				stu +="</tr>";
			}
			if(judge == 1){
				stu3 = "<button type='button' class='btn btn-primary'  onclick='soldOut()'>下架</button>"
			}else{
				stu3 = "<button type='button' class='btn btn-primary'  onclick='addquantity()'>添加</button>"
			}
			
			
			$("#button").empty( );
			$("#theme").empty( );
			$("#infTable").empty( );
			$("#button").html(stu3);
			$("#theme").html(stu2);
			$("#infTable").html(stu);
		},
		error:function(){
			alert("系统故障")
		},
	})
}

//查询超市商品
function selsectSupermarket(){
	
	var product = "";
	var classify = "";
	product = $("#product").val();
	classify = $("#classify").val();
	window.location.href="selsectSupermarket2.do?product="+product+"&classify="+classify;
}


</script>
<body>
<hr><h1 align="center">超市</h1><hr>
<!-- 条件查询部分 -->
<table >
<tr><td><input type="text" name="product" id="product" class="form-control" placeholder="商品名称" value=""></td>
<td><input type="text" name="classify" id="classify" class="form-control" placeholder="请输入商品分类" value=""></td>
<td>&nbsp;&nbsp;<button class="btn btn-primary" onclick="selsectSupermarket()">查询</button>&nbsp;&nbsp; </td>
</tr>
</table>
<br>
<!-- 查询信息显示部分 -->
	<table class="table table-hover">
		<tr>
			<th>商品名称</th>
			<th>商品分类</th>
			<th>单价</th>
			<th>剩余商品数量</th>
			<th>备注</th>
			<th>操作</th>
		</tr>
		<c:forEach items="${pageInfo.list}" var="superist">
			<tr id="${superist.reserved1}">
				<td>${superist.product}</td>
				<td>${superist.classify}</td>
				<td>${superist.price}￥</td>
				<td>${superist.quantity}</td>
				<td>${superist.remark}</td>
				<td><a href="#"
					onclick="queryProutInf('${superist.reserved1}',1)"
					data-toggle="modal" data-target="#myModal">下架</a>
					<a href="#"
					onclick="queryProutInf('${superist.reserved1}',2)"
					data-toggle="modal" data-target="#myModal">增加商品数量</a>
					</td>
			</tr>
		</c:forEach>
	</table>

	<c:if test="${inf eq null}">
		<div id="page1">
	<c:if test="${!pageInfo.isFirstPage }">
					<a href="${pageContext.request.contextPath }/querySupermarketAll.do">首页</a>
					<a href="${pageContext.request.contextPath }/querySupermarketAll.do?pageNum=${pageInfo.prePage}">上一页</a>
				</c:if>
				<c:if test="${!pageInfo.isLastPage }">
				<a href="${pageContext.request.contextPath }/querySupermarketAll.do?pageNum=${pageInfo.nextPage}">下一页</a>
				<a href="${pageContext.request.contextPath }/querySupermarketAll.do?pageNum=${pageInfo.lastPage}">末页</a>
				</c:if>
				当前页：${pageInfo.pageNum }/${pageInfo.pages }.
				</div>
	</c:if>
	
	    <c:if test="${!(inf eq null)}">
		<div id="page1">
	<c:if test="${!pageInfo.isFirstPage }">
					<a href="${pageContext.request.contextPath }/selsectSupermarket2.do?product=${product}&classify=${classify}">首页</a>
					<a href="${pageContext.request.contextPath }/selsectSupermarket2.do?pageNum=${pageInfo.prePage}">上一页</a>
				</c:if>
				<c:if test="${!pageInfo.isLastPage }">
				<a href="${pageContext.request.contextPath }/selsectSupermarket2.do?pageNum=${pageInfo.nextPage}&product=${product}&classify=${classify}">下一页</a>
				<a href="${pageContext.request.contextPath }/selsectSupermarket2.do?pageNum=${pageInfo.lastPage}&product=${product}&classify=${classify}">末页</a>
				</c:if>
				当前页：${pageInfo.pageNum }/${pageInfo.pages }.
				</div>
	   </c:if>


				<!-- 模态框（Modal） -->
		<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header" >
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
					</div >
					<div id="theme"></div>
					
					<div >
					<table class="table table-bordered" id="infTable"></table>
					
					</div>
					<div id="quantity">
					
					</div>
					
					<div class="modal-footer" id="button">
						
					</div>
				</div><!-- /.modal-content -->
			</div><!-- /.modal -->
		</div>
</body>
</html>